<?php
namespace App\Models\ActivityRecord;

use App\Contracts\Repositories\ActivityRecordRepository as ActivityRecordRepositoryContract;
use Illuminate\Support\Facades\Log;

class ActivityRecordRepository implements ActivityRecordRepositoryContract
{
    public function create($activityRecord)
    {
        try {
            return ActivityRecord::create($activityRecord);
        } catch (\Exception $e) {
            Log::info($e->getMessage());
        }
    }

    public function getActivityRecord($activityId)
    {
        return ActivityRecord::where('id', $activityId)->get();
    }

    /**
     * @param $activityId
     * @param null | [Array] $created_at 时间范围查询
     * @return ActivityRecord[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getActivityRecordsByIdOrTime($activityId,$created_at=null)
    {
        if($created_at == null) {
            return ActivityRecord::where('activity_id', $activityId)->get();
        }else{
            return ActivityRecord::where('activity_id',$activityId)->whereBetween('created_at',$created_at)->get();
        }
    }

    public function countUserByConditions($conditions)
    {
        return ActivityRecord::query()->select('openid')->where($conditions)->groupBy('openid')->get()->count();
    }

    public function findByConditionsPage($condition, $sortArray=array(), $start = 0, $limit = 10)
    {
        // TODO: Implement findByConditionsPage() method.
        $qb = ActivityRecord::with('logistics', 'logistics.company')
            ->where($condition);

        if ($limit > 0) {
            $qb->limit($limit);
            $qb->skip($start);
        }

        if ($sortArray) {
            foreach ($sortArray as $sort) {
                $qb = $qb->orderBy($sort[0], $sort[1]);
            }
        } else {
            $qb = $qb->orderBy('created_at', 'ASC');
        }

        return $qb->get();
    }

    public function countByConditions($conditions)
    {
        $qb = ActivityRecord::query();
        if (is_array($conditions)) {
            foreach ($conditions as $row) {
                if (count($row) == 2) {
                    if (is_array($row[1])) {
                        $qb = $qb->whereIn($row[0], $row[1]);
                    } else {
                        $qb = $qb->where([$row]);
                    }
                } else {
                    $qb = $qb->where([$row]);
                }
            }
        } else {
            $qb = $qb->where($conditions);
        }
        return $qb->get()->count();
    }

    public function updateRecord($updates, $conditions)
    {
        return ActivityRecord::query()->where($conditions)->update($updates);
    }
    public function updateRecords($updates, $conditions)
    {
        return ActivityRecord::query()->whereIn('id',$conditions)->update($updates);
    }

    public function findByConditions($conditions, $sort)
    {
        $qb =  ActivityRecord::query()->where($conditions);
        if (!empty($sort) && is_array($sort)) {
            $qb = $qb->orderBy($sort[0], $sort[1]);
        }
        return $qb->get();
    }
}
